import os
import json
from recongnize import damerau_levenshtein_distance
if __name__ == "__main__":

    img_dir = "E:/internship/dataset_test/cloud/indi_doc/images_crop"
    fpre = open(os.path.join(img_dir, "img-results.txt"), "r", encoding="utf-8")
    fgdt = open(os.path.join(img_dir, "Label.txt"), "r", encoding="utf-8")
    # ==================================================================================
    fgdt_lines = fgdt.readlines()
    gdt_dict = dict()
    for line in fgdt_lines:
        line = line.strip("\n")
        text = line.split("\t")[1].strip("\n")
        text = text.strip()
        key = line.split("\t")[0].split(".")[0]
        gdt_dict[key] = text
    fpre_lines = fpre.readlines()
    pre_dict = dict()
    for line in fpre_lines:
        line = line.strip("\n")
        text = line.split("\t")[1].strip("\n")
        text = text.strip()
        key = line.split("\t")[0].split(".")[0]
        pre_dict[key] = text
    from tqdm import tqdm
    len_gts = 0.0
    scores = 0.0
    correctnum = 0
    if os.path.exists(os.path.join(img_dir, "diff.txt")) :
        os.remove(os.path.join(img_dir, "diff.txt"))
    if os.path.exists(os.path.join(img_dir, "diff_simple.csv")) :
        os.remove(os.path.join(img_dir, "diff_simple.csv"))
    fwdiff_simple = open(os.path.join(img_dir, "diff_simple.csv"), 'a', encoding="utf-8")
    fwdiff_simple.write("filename,edit_score" + "\n")
    fwdiff_simple.close()
    for i, key in enumerate(tqdm(list(gdt_dict.keys()))):
        fw = open(os.path.join(img_dir, "diff.txt"), 'a', encoding="utf-8")
        fw.write(key + "\n")
        fw.close()
        len_gts = len_gts + len(gdt_dict[key])
        dis = damerau_levenshtein_distance(pre_dict[key], gdt_dict[key], len1=8, diff_dir=img_dir, print_log=True)

        fwdiff_simple = open(os.path.join(img_dir, "diff_simple.csv"), 'a', encoding="utf-8")
        score = 0
        if 1.0 - dis / len(gdt_dict[key]) > 0:
            score = 1.0 - dis / len(gdt_dict[key])
        fwdiff_simple.write(key + "," + str(score) + "\n")
        fwdiff_simple.close()
        if score == 1:
            correctnum = correctnum + 1.0
        scores = scores + score
    fw = open(os.path.join(img_dir, "diff.txt"), 'a', encoding="utf-8")
    fw.write("\n\n")
    fw.write("total " + str(len(gdt_dict)) + "\n")
    fw.write("correct  " + str(correctnum) + "\n")

    fw.write("line acc = " + str(correctnum / len(gdt_dict)) + "\n")
    fw.write("char acc = " + str(scores/len(gdt_dict)) + "\n")
    fw.close()
    print("line acc = ", str(correctnum / len(gdt_dict)))
    print("char acc = ", str(scores/len(gdt_dict)))

    fwdiff_simple = open(os.path.join(img_dir, "diff_simple.csv"), 'a', encoding="utf-8")
    fwdiff_simple.write("\n\ntotal," + str(len(gdt_dict)) + "\n")
    fwdiff_simple.write("correct," + str(correctnum) + "\n")

    fwdiff_simple.write("line acc," + str(correctnum / len(gdt_dict)) + "\n")
    fwdiff_simple.write("char acc," + str(scores / len(gdt_dict)) + "\n")
    fwdiff_simple.close()